home *** CD-ROM | disk | FTP | other *** search
/ Belgian Amiga Club - ADF Collection / BS1 part 41.zip / BS1 part 41 / Compute`s Amiga resource 1.adf / Source / ALC / advdoor.c next >
C/C++ Source or Header  |  1989-02-07  |  8KB  |  298 lines

  1. /* Advanced Laser Chess initialization/cleanup module
  2.  * This module contains all routines devoted to opening and closing
  3.  * (initializing/allocating and cleaning up/deallocating) the items
  4.  * (memory, screens, bitmaps, etc.) necessary for the game. */
  5.  
  6. #include "advlaser.h"
  7. #include "adviff.h"
  8.  
  9.  
  10.  
  11. /* External variables */
  12. extern struct Screen *screen;          /* From ADVGRAPH.C */
  13. extern struct NewScreen screen_def;    /* ADVGRAPH.C */
  14. extern struct NewWindow window_def;    /* ADVGRAPH.C */
  15. extern struct Window *window;          /* ADVGRAPH.C */
  16. extern struct BitMap *current_bitmap;  /* ADVGRAPH.C */
  17. extern struct BitMap image_bitmap;     /* ADVGRAPH.C */
  18. extern struct BitMap primary_bitmap;   /* ADVGRAPH.C */
  19. extern struct BitMap backup_bitmap;    /* ADVGRAPH.C */
  20. extern struct BitMap mask_bitmap;      /* ADVGRAPH.C */
  21. extern char game_name[GAMENAMELENGTH]; /* From ADVLASER.C */
  22. extern char default_game[GAMENAMELENGTH]; /* ADVLASER.C */
  23. extern void *allocate_mem();           /* ADVLASER.C */
  24.  
  25. /* Local variables */
  26. struct IntuitionBase *IntuitionBase;
  27. struct GfxBase *GfxBase;
  28. struct ColorMap *color_map = NULL;
  29. UWORD *sprite = NULL;
  30. int clup;
  31.  
  32.  
  33.  
  34. one_time_init()  /* One-time initialization */
  35. {
  36.   open_libraries();
  37.   open_screen();
  38.   set_pointer();
  39.   load_pieces();
  40.   if(!get_bitmap(&backup_bitmap))
  41.     cleanup(NOMEM);
  42.   init_sound();
  43.   strcpy(game_name, DEFAULTGAMENAME);
  44.   strcpy(default_game, DEFAULTGAMENAME);
  45. }
  46.  
  47.  
  48.  
  49. open_libraries()
  50. {
  51.   if((IntuitionBase = (struct IntuitionBase *)
  52.   OpenLibrary("intuition.library", 1L)) == NULL)
  53.     cleanup(NOINTUILIB);
  54.  
  55.   if((GfxBase = (struct GfxBase *)
  56.   OpenLibrary("graphics.library", 1L)) == NULL)
  57.     cleanup(NOGRAPHLIB);
  58. }
  59.  
  60.  
  61.  
  62. open_screen()
  63. {
  64.   register int i;
  65.  
  66.   /* Initialize bitmaps */
  67.   if(!get_bitmap(&primary_bitmap))
  68.     cleanup(NOMEM);
  69.  
  70.   for(i=0; i<PLANES; i++)
  71.     mask_bitmap.Planes[i] = NULL;  /* Define to known state */
  72.   InitBitMap(&mask_bitmap, (long)PLANES, (long)((MASKWIDTH + 15) / 8),
  73.     (long)MASKHEIGHT);
  74.   for(i=0; i<PLANES; i++)
  75.     if((mask_bitmap.Planes[i] =
  76.     allocate_mem((long)((MASKWIDTH + 15) / 8 * MASKHEIGHT), MEMF_CHIP)) == NULL)
  77.       cleanup(NOMEM);
  78.  
  79.   current_bitmap = &primary_bitmap;
  80.   screen_def.CustomBitMap = &primary_bitmap; /* Set initial bitmap pointer */
  81.   if((screen = OpenScreen(&screen_def)) == NULL) /* Attempt to open screen */
  82.     cleanup(NOSCREEN);
  83.  
  84.   color_map = screen->ViewPort.ColorMap;
  85.   screen->ViewPort.ColorMap = NULL;  /* Reset in case of error below */
  86.   if( (screen->ViewPort.ColorMap = GetColorMap(32L) ) == NULL)
  87.     cleanup(NOMEM);
  88.  
  89.   screen->ViewPort.DyOffset -= 4;
  90.   RethinkDisplay();
  91.  
  92.   window_def.Screen = screen;
  93.   if((window = OpenWindow(&window_def)) == NULL)
  94.     cleanup(NOWINDOW);
  95.   clear_screen(&primary_bitmap);
  96.   SetAPen(&screen->RastPort, WHITE);
  97.   Move(&screen->RastPort, 120L, 95L);
  98.   Text(&screen->RastPort, "Hold on...", 10L);
  99. }
  100.  
  101.  
  102.  
  103. set_pointer()        /* Defines custom pointer image */
  104. {
  105.   register int i;
  106.   static UWORD sprite_data[] =
  107.     {0x4000, 0x4000, 0x6000, 0xa000, 0x7000, 0x9000, 0x7800, 0x8800,
  108.      0x7c00, 0x8400, 0x7000, 0x8c00, 0x4000, 0xb000, 0x0000, 0xc000};
  109.  
  110.   if((sprite = allocate_mem((long)(8 + 4 * POINTERHEIGHT),
  111.   MEMF_CHIP | MEMF_CLEAR)) == NULL)
  112.     cleanup(NOMEM);
  113.   for(i=0; i<POINTERHEIGHT * 2; i++)
  114.     sprite[i + 2] = sprite_data[i];
  115.   SetPointer(window, sprite, (long)POINTERHEIGHT, (long)POINTERWIDTH, -2L, 0L);
  116.   SetRGB4(&screen->ViewPort, 18L, 0L, 0L, 0L); /* Pointer shadow is black */
  117. }
  118.  
  119.  
  120.  
  121. get_bitmap(bitmap)
  122. register struct BitMap *bitmap;
  123. {
  124.   register int i;
  125.  
  126.   for(i=0; i<PLANES; i++)
  127.     bitmap->Planes[i] = NULL;  /* Predefine to known state */
  128.   InitBitMap(bitmap, (long)PLANES, (long)SCREENX, (long)SCREENY);
  129.   for(i=0; i<PLANES; i++) /* Allocate memory for bitmap planes */
  130.   {
  131.     if((bitmap->Planes[i] =
  132.     allocate_mem((long)(SCREENX / 8 * SCREENY), MEMF_CHIP)) == NULL)
  133.       return(FALSE);
  134.   }
  135.   return(TRUE);
  136. }
  137.  
  138.  
  139.  
  140. load_pieces()
  141. {
  142.   register int i;
  143.   struct iff_header iffh;
  144.  
  145.   /* Prepare image bitmap */
  146.   InitBitMap(&image_bitmap, (long)PLANES, (long)SCREENX, (long)SCREENY);
  147.   for(i=0; i<PLANES; i++) /* Allocate memory for image screen */
  148.     if((image_bitmap.Planes[i] =
  149.     allocate_mem((long)(SCREENX / 8 * SCREENY), MEMF_CHIP)) == NULL)
  150.       cleanup(NOMEM);
  151.  
  152.   /* Load the IFF file into the RAM allocated in image_bitmap */
  153.   if((read_iff_header(IMAGEFILE, &iffh)) != RIFFH_SUCCESS)
  154.     cleanup(NOIFF);
  155.   if((load_iff(&iffh, IMAGEFILE, screen->ViewPort.ColorMap->ColorTable,
  156.   &image_bitmap)) != LIFF_SUCCESS)
  157.     cleanup(NOIFF);
  158.   /* Use the colors associated with the image file */
  159.   LoadRGB4(&screen->ViewPort, screen->ViewPort.ColorMap->ColorTable,
  160.   (long)SCREENCOLORS);
  161. }
  162.  
  163.  
  164.  
  165. close_libraries() /* Closes all opened libraries */
  166. {
  167.   if(IntuitionBase != NULL) CloseLibrary(IntuitionBase);
  168.   if(GfxBase != NULL) CloseLibrary(GfxBase);
  169. }
  170.  
  171.  
  172.  
  173. free_memory()   /* Release memory previously allocated */
  174. {
  175.   register int i;
  176.  
  177.   for(i=0; i<PLANES; i++)    /* Free bitmap memory */
  178.   {
  179.     if(image_bitmap.Planes[i] != NULL)
  180.       FreeMem(image_bitmap.Planes[i], (long)(SCREENX / 8 * SCREENY));
  181.     if(primary_bitmap.Planes[i] != NULL)
  182.       FreeMem(primary_bitmap.Planes[i], (long)(SCREENX / 8 * SCREENY));
  183.     if(backup_bitmap.Planes[i] != NULL)
  184.       FreeMem(backup_bitmap.Planes[i], (long)(SCREENX / 8 * SCREENY));
  185.     if(mask_bitmap.Planes[i] != NULL)
  186.       FreeMem(mask_bitmap.Planes[i],
  187.         (long)((MASKWIDTH + 15) / 8 * MASKHEIGHT));
  188.   }
  189.   if(sprite != NULL)
  190.     FreeMem(sprite, (long)(8 + 4 * POINTERHEIGHT));
  191. }
  192.  
  193.  
  194.  
  195. display_error(error)
  196. register int error;
  197. {
  198.   static char *okay = "Ok";
  199.   static char *nolib[] =
  200.   {
  201.     "There was a problem attempting",
  202.     "to open one of the system",
  203.     "\"libraries\"; make sure all",
  204.     "of the required files are on",
  205.     "your Workbench disk."
  206.   };
  207.   static char *nomem[] =
  208.   {
  209.     "There isn't enough memory!",
  210.     "Close some existing programs",
  211.     "or delete some files from",
  212.     "RAM, then try running",
  213.     "Advanced Laser Chess again."
  214.   };
  215.   static char *noscreen[] =
  216.   {
  217.     "A screen couldn't be opened!"
  218.   };
  219.   static char *nowindow[] =
  220.   {
  221.     "A window couldn't be opened!"
  222.   };
  223.   static char *noiff[] =
  224.   {
  225.     "There was a problem",
  226.     "reading the file",
  227.     IMAGEFILE
  228.   };
  229.   static char *noaudiodev[] =
  230.   {
  231.     "The audio device could not",
  232.     "be opened."
  233.   };
  234.   static char *noreplyport[] =
  235.   {
  236.     "A required \"port\" could not",
  237.     "be opened for communication",
  238.     "with the audio device."
  239.   };
  240.  
  241.   switch(error)
  242.   {
  243.     case NOINTUILIB:
  244.     case NOGRAPHLIB:
  245.       req(nolib, okay, okay, 5, FALSE);
  246.       break;
  247.     case NOMEM:
  248.       req(nomem, okay, okay, 5, FALSE);
  249.       break;
  250.     case NOSCREEN:
  251.       req(noscreen, okay, okay, 1, FALSE);
  252.       break;
  253.     case NOWINDOW:
  254.       req(nowindow, okay, okay, 1, FALSE);
  255.       break;
  256.     case NOIFF:
  257.       req(noiff, okay, okay, 3, FALSE);
  258.       break;
  259.     case NOAUDIODEVICE:
  260.       req(noaudiodev, okay, okay, 2, FALSE);
  261.       break;
  262.     case NOAUDIOREPLYPORT:
  263.       req(noreplyport, okay, okay, 3, FALSE);
  264.       break;
  265.   }
  266. }
  267.  
  268.  
  269.  
  270. cleanup(error)  /* Closes up the program (cleanly) */
  271. int error;
  272. {
  273.   if(error != NOERRORS)
  274.     display_error(error);
  275.  
  276.   close_sound();
  277.   if(window != NULL)
  278.   {
  279.     dequeue_window();  /* Remove any pending messages at window's IDCMP */
  280.     CloseWindow(window);
  281.   }
  282.  
  283.   if(screen != NULL)
  284.   {
  285.     if(screen->ViewPort.ColorMap != NULL)
  286.       FreeColorMap(screen->ViewPort.ColorMap);
  287.     screen->ViewPort.ColorMap = color_map;
  288.     CloseScreen(screen);
  289.   }
  290.  
  291.   free_memory();
  292.  
  293.   close_libraries();
  294.  
  295.   exit(0);
  296. }
  297.  
  298.